package com.snk.controller;

import com.snk.common.config.Cache;
import com.snk.common.pojo.JsonResult;
import com.snk.common.pojo.PageObject;
import com.snk.common.utils.ObjectMapperUtil;
import com.snk.pojo.Cart;
import com.snk.pojo.SysReason;
import com.snk.pojo.User;
import com.snk.service.CartService;
import com.snk.util.UserThreadLocal;
import org.apache.tomcat.util.buf.StringCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    private static final String SNK_TICKET="SNK_TICKET";

    @Autowired
    private CartService cartService;

    @RequestMapping("/show")
    @ResponseBody
    public JsonResult findCartList(){
        long userId =Long.valueOf(UserThreadLocal.getUser());//暂时写死,后期维护
        System.out.println(userId);
        System.out.println(userId);
        PageObject<Cart> cartList = cartService.findCartList(userId);
        return new JsonResult(cartList);
    }

    /**
     * 完成购物车入库操作
     * url:http://www.nb.com/cart/save/968184
     * */
    @RequestMapping("/save/{itemId}")
    public String saveCart(Cart cart){
        long userId =Long.valueOf(UserThreadLocal.getUser());
       // long userId =UserThreadLocal.getUser().getId();
        System.out.println(userId);
        cart.setUserId(userId);
        cartService.saveCart(cart);
        return "redirect:/index";
    }
    /**
     * 购物车数量增减
     * 需要商品id和增减数量
     * 需要参数 itemId=？ num=1是增加数量 -1是减少数量
     * */
    @RequestMapping("???")
    public void updateCart(@PathVariable Long itemId,
                           @PathVariable Integer num){
        Long userId=Long.valueOf(UserThreadLocal.getUser());
        Cart cart = new Cart();
        cart.setItemId(itemId).setNum(num).setUserId(userId);
        System.out.println("订单====数量====更新===对象"+cart);
        cartService.updateCart(cart);
    }
}
